﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quality.BasicData
{
    public partial class testtypesedit : Form
    {
        public String id { get; set; }
        public testtypesedit(string id)
        {
            this.id = id;
            InitializeComponent();
            DB.getcombobox(classnum, "testclass", "num", "classname");
        }

        private void testtypesedit_Load(object sender, EventArgs e)
        {
            if (id == "")
            {
                num.Text = "";
                typename.Text = "";
               

            }
            else
            {
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand();

                da.SelectCommand.CommandText = "select * from  testtypes where id=@id";
                da.SelectCommand.Parameters.AddWithValue("@id", id);
                DataSet dt = DB.getlist(da);
                DataRow row = dt.Tables[0].Rows[0];
                num.Text = row["num"].ToString();
                typename.Text = row["typename"].ToString();
                classnum.SelectedValue = row["classnum"].ToString();

            }
        }

       
        public event TransfDelegate1 TransfEvent;
        private void savebutton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(num.Text)) { MessageBox.Show("编号不能为空！"); return; }
            if (string.IsNullOrEmpty(typename.Text)) { MessageBox.Show("类型名称不能为空！"); return; }
            if (classnum.SelectedIndex==-1) { MessageBox.Show("分类不能为空！"); return; }
         
            SqlCommand comm = new SqlCommand();
            if (id == "")
            {
                int count = sqlHelper.Count("select * from testtypes  where num='" + num.Text + "'");
                if (count > 0) { MessageBox.Show("编号重复，不能保存！"); return; }
                comm.CommandText = "insert into testtypes(num,typename,classnum)values(@num,@typename,@classnum)";
            }
            else
            {

                comm.CommandText = "update testtypes set num=@num,typename=@typename,classnum=@classnum where id=@id";


            }


            //传值 username，不指定参数长度
            //查询计划为(@UserName varchar(8))select * from Users where UserName=@UserName
            // comm.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar) { Value = "username" });
            comm.Parameters.AddWithValue("@id", id);
            comm.Parameters.AddWithValue("@num", num.Text);
            comm.Parameters.AddWithValue("@typename", typename.Text);
           
            comm.Parameters.AddWithValue("@classnum", (classnum.SelectedIndex == -1)? "NULL":classnum.SelectedValue);
           
            DB.dosql(comm);
            TransfEvent();
            MessageBox.Show("保存成功！");
            this.Close();
        }
    }
}
